package com.pangolin.pangolinim.ui.activity;

import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.lqr.optionitemview.OptionItemView;
import com.orhanobut.logger.Logger;
import com.pangolin.pangolinim.R;
import com.pangolin.pangolinim.app.BroadcastMsg;
import com.pangolin.pangolinim.ui.adapter.UIContact;
import com.pangolin.pangolinim.ui.base.BaseActivity;
import com.pangolin.pangolinim.utils.BundleUtil;
import com.pangolin.pangolinim.utils.GsonUtil;
import com.pangolin.pangolinim.utils.ImageUtil;
import com.pangolin.pangolinim.utils.UIUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class SessionInfoActivity extends BaseActivity {
    private UIContact me;
    private UIContact contact;

    private ImageView mIvHeader;
    private TextView mTvName;
    private TextView mTvUid;

    private OptionItemView mOivClearMsgRecord;

    @Override
    protected void init(){
        String strMe = getIntent().getStringExtra("param1");
        String strContact = getIntent().getStringExtra("param2");
        if(strContact != null && strMe != null){
            contact = GsonUtil.fromJson(strContact , UIContact.class);
            me = GsonUtil.fromJson(strMe , UIContact.class);
            Logger.d("me:%d|%s\ncontact:%d|%s" ,me.getUid() , me.getName() , contact.getUid() , contact.getName());
        }
    }

    @Override
    protected void initView(){
        mIvHeader = findViewById(R.id.ivHeader);
        mTvName = findViewById(R.id.tvName);
        mTvUid = findViewById(R.id.tvUid);
        mOivClearMsgRecord = findViewById(R.id.oivClearMsgRecord);

        if(contact.getBitHeader() != null ){
            mIvHeader.setImageBitmap(contact.getBitHeader());
        }
        mTvUid.setText("UID:" + String.valueOf(contact.getUid()));
        mTvName.setText(contact.getName());
    }

    @Override
    protected void initListener(){
        mOivClearMsgRecord.setOnClickListener(v -> {
            //清空聊天记录
            showMaterialDialog(UIUtil.getString(R.string.clear_chat_log),
                    UIUtil.getString(R.string.clear_chat_log_content),
                    UIUtil.getString(R.string.delete),
                    UIUtil.getString(R.string.cancel),
                    v1 -> {
                        hideMaterialDialog();
                        deleteChatlog(contact.getUid());
                    },
                    v2 -> hideMaterialDialog());
        });
    }

    @Override
    protected void CoreMessage(BundleUtil bundleUtil){
        if(bundleUtil.getType() == BroadcastMsg.SIG_CORE_DATA){
            if(bundleUtil.getData() != null){
                Map<String, Object> resMap = bundleUtil.getData();
                Logger.d(resMap);
                if((int) resMap.get("type") == BroadcastMsg.DATA_CORE_DELLOG){
                    if((int)resMap.get("result") == 0){
                        UIUtil.showToast("已清空记录");
                    }else{
                        UIUtil.showToast((String) resMap.get("msg"));
                    }
                }
            }
        }
    }

    @Override
    protected int provideContentViewId(){
        return R.layout.activity_session_info;
    }

    private void deleteChatlog(int uid){
        Map<String, Object> brocastMap = new HashMap<String, Object>();
        brocastMap.put("type" , BroadcastMsg.DATA_UI_DELLOG);
        brocastMap.put("uid" , uid);
        BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_UI_DATA , brocastMap , null);
        BroadcastSender(bundleUtil.getBundle());
    }
}